
import java.util.*;

public class WhatBot {
    // The DoTurn function is where your code goes. The PlanetWars object
    // contains the state of the game, including information about all planets
    // and fleets that currently exist. Inside this function, you issue orders
    // using the pw.IssueOrder() function. For example, to send 10 ships from
    // planet 3 to planet 8, you would say pw.IssueOrder(3, 8, 10).
    //
    // There is already a basic strategy in place here. You can use it as a
    // starting point, or you can throw it out entirely and replace it with
    // your own. Check out the tutorials and articles on the contest website at
    // http://www.ai-contest.com/resources.

    public static void DoTurn(PlanetWars pw, long startTime) {
        double strengthWeight = .1;
        double growthWeight = 30.0;
        double distanceWeight = 10.0;
        int iterations = 0;
        int planetsToTarget = 3;
        List<Integer> targetPlanets = new ArrayList<Integer>();
        List<Integer> planetsAttackedThisTurn = new ArrayList<Integer>();
        for (Fleet f : pw.MyFleets()) {
            if (!targetPlanets.contains(f.DestinationPlanet())) {
                targetPlanets.add(f.DestinationPlanet());
            }
        }
        boolean orderIssued = true;
        pw.Log("#Turn: " + pw.Turn());
        //loop through until you run out of time or have too many targets
        while ((System.nanoTime() - startTime) < 100000000 && targetPlanets.size() < planetsToTarget && orderIssued) {
            strengthWeight = 10.0 / pw.NumShips(1);
            orderIssued = false;
            iterations += 1;

            pw.Log("#Iteration: " + String.valueOf(iterations));
            pw.Log("#Time_elapsed: " + (System.nanoTime() - startTime));

            boolean attackMode = false;
            boolean spreadMode = false;
//            if (pw.Turn() < 30 && pw.Planets().size() / 2 > pw.EnemyPlanets().size()) {
//                spreadMode = true;
//                planetsToTarget = 10;
//            }
            if (pw.NumShips(1) > pw.NumShips(2)) {
                if (pw.Production(1) > pw.Production(2) * 1.5) {
                    planetsToTarget = 3;
                    attackMode = true;
                } else {
                    planetsToTarget = 8;
                }
            } else {
                if (pw.Production(1) > pw.Production(2)) {
                    planetsToTarget = 3;
                } else {
                    planetsToTarget = 8;
                }
            }

            if (planetsToTarget > pw.Planets().size()) {
                planetsToTarget = pw.Planets().size();
            }

            // (1) If we currently have more ships in flight than on planets,
            // do nothing.
            if (pw.NumShipsOnPlanets(1) <= pw.NumShipsInFlight(1)) {
                return;
            }
            Planet source = null;
            Planet dest = null;
            // (2) Find my strongest planet.
            double sourceScore = Double.MIN_VALUE;
            for (Planet p : pw.MyPlanets()) {
                double score = (double) pw.PlanetNetStrength(p) / (1 + p.GrowthRate());
                if (score > sourceScore) {
                    sourceScore = score;
                    source = p;
                }
            }

            if (source != null) {
                // (3) Find the fastest growing enemy or neutral planet.

                List<Planet> candidates = pw.NotMyPlanets();
                if (attackMode) {
                    candidates = pw.EnemyPlanets();
                } else if (spreadMode) {
                    candidates = pw.NeutralPlanets();
                }
                candidates = pw.PlanetsSortedByValue(source, candidates, distanceWeight, growthWeight, strengthWeight);

                for (Planet p : candidates) {
                    if (pw.PlanetNetStrength(p) < (pw.PlanetNetStrength(source))) {
                        dest = p;
                        targetPlanets.add(dest.PlanetID());
                        planetsAttackedThisTurn.add(dest.PlanetID());
                        break;
                    }
                }
                if (dest == null) {
                    dest = pw.ClosestTo(source, candidates);
                    if (dest != null) {
                        targetPlanets.add(dest.PlanetID());
                        planetsAttackedThisTurn.add(dest.PlanetID());
                    }

                }
            }

            // (4) Send half the ships from my strongest planet to the weakest
            // planet that I do not own.
            if (source != null && dest != null) {
                boolean issuedOrder = false;
                int numShips = Math.abs(pw.PlanetNetStrengthPlusGrowthTimesDistance(source, dest)) + 2;
                //                for (int i=0; i<numShips; i++) {
                //                    pw.CacheOrder(source, dest, 1);
                //                }
                if (pw.PlanetNetStrength(source) - numShips > 1 && numShips < source.NumShips()) {
                    issuedOrder = true;
                    orderIssued = true;
                    pw.CacheOrder(source, dest, numShips);
                }
                if (!issuedOrder) {
                    for (Planet p : pw.NeutralPlanets()) {
                        if (pw.PlanetNetStrengthPlusGrowthTimesDistance(source, p) < 0) {
                            numShips = Math.abs(pw.PlanetNetStrengthPlusGrowthTimesDistance(source, p)) + 2;
                            if (pw.PlanetNetStrength(source) - numShips > 1 && numShips < source.NumShips()) {
                                pw.CacheOrder(source, p, numShips);
                            }
                        }
                    }
                }
            }

            // (5) Reinforce planets in trouble.
            for (Planet p : pw.MyPlanets()) {
                if (pw.PlanetNetStrength(p) <= 0) {
                    // (2) Find my strongest planet.
                    Planet source1 = null;
                    double sourceScore1 = Double.MIN_VALUE;
                    for (Planet p1 : pw.MyPlanets()) {
                        double score1 = (double) p1.NumShips() / (1 + p1.GrowthRate());
                        if (score1 > sourceScore1) {
                            sourceScore1 = score1;
                            source1 = p1;
                        }
                    }
                    if (source1 != null) {
                        int numShips = Math.abs(pw.PlanetNetStrengthPlusGrowthTimesDistance(source1, p)) + 5;
                        //                for (int i=0; i<numShips; i++) {
                        //                    pw.CacheOrder(source, dest, 1);
                        //                }
                        if (pw.PlanetNetStrength(source1) - numShips > 1 && numShips < source1.NumShips()) {
                            orderIssued = true;
                            pw.CacheOrder(source1, p, numShips);
                        }
                    }
                }

            }
        }

    }

    public static void main(String[] args) {
        PlanetWars pw = new PlanetWars();
        String line = "";
        String message = "";
        int c;
        try {
            while ((c = System.in.read()) >= 0) {
                switch (c) {
                    case '\n':
                        if (line.equals("go")) {
                            long startTime = System.nanoTime();
                            pw.ParseGameState(message);
                            DoTurn(pw, startTime);
                            pw.IssueCachedOrders();
                            pw.FinishTurn();
                            message = "";
                        } else {
                            message += line + "\n";
                        }
                        line = "";
                        break;
                    default:
                        line += (char) c;
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace(System.out);
            // Owned.
        }
    }
}
